﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MaxLeaf.DB;
using MaxLeaf.Common;
using System.Data.SqlClient;
using System.Data;

namespace MaxLeaf.DAL.K2K
{
    

    //上级组
    public class BaseGroupDAL : BaseDAL
    {
        protected string strGroupTableName = null;
        
        public BaseGroupDAL()
            : base()
        {
            strTableName = "t_Item";
            strFieldNames = @"";
            
            
        }

        
        public override int  Update(DBAccess dbOrgK3Con, DBAccess dbDestK3Con, string strKeyValue)
        {
            DataRow drOrgData = dbOrgK3Con.GetTableDataFirstRow("t_item", new Dictionary<string, object> { { "FitemID", strKeyValue } }, "Fnumber");
            string[] arrFnumber = CvtFunc.ToString(drOrgData["Fnumber"]).Split('.');
            return CvtFunc.ToInt(GetFParentID(dbOrgK3Con, dbDestK3Con, arrFnumber));
            

        }

        private string GetFParentID(DBAccess dbOrgK3Con, DBAccess dbDestK3Con, string[] arrFnumber)
        {
            const int intFDetail = 0;
            string strFItemId = string.Empty;
            t_ItemDAL dal = new t_ItemDAL(strFItemClassID, strUpdateFNumberTableName);
            for (int i = 0; i < arrFnumber.Length; i++)
            {
                string strFParentID = "0";
                if (i != 0)
                {
                    strFParentID = strFItemId;
                }
                //得到每级的FitemID
                strFItemId = dal.GetFItemId(dbOrgK3Con, dbDestK3Con, i + 1,
                    string.Empty, strFParentID, string.Join(".", arrFnumber, 0, i + 1), intFDetail);
                 
                    UpdateAccess(dbDestK3Con, strFItemId, strFParentID);
                 



            }

            return strFItemId;
        }



   
        private void UpdateAccess (DBAccess dbDestK3Con, string strFItemId, string strFParentID)
        {
            string strSql = string.Format(@"
                if not exists(select * from access_{0} where FitemID=@FitemID)
                begin
                 Insert into access_{0}(FitemID,FParentIDX,FDataAccessView,FDataAccessEdit,FDataAccessDelete)
                 Values(@FitemID,@FParentIDX,convert(varbinary(7200),REPLICATE(char(255),100)),convert(varbinary(7200),REPLICATE(char(255),100)),convert(varbinary(7200),REPLICATE(char(255),100)))

                end
                ",strGroupTableName);
            dbDestK3Con.ExecuteSql(strSql, new SqlParameter[] { new SqlParameter("@FitemID", strFItemId), new SqlParameter("@FParentIDX", strFParentID) });
        }

       

        public override string GetDestMapKeyValue(DBAccess dbOrgK3Con, DBAccess dbDestK3Con, string strOrgFItemID)
        {
            if (dbDestK3Con.ServerMasterDB)
            {
                return strOrgFItemID;
            }

            KeyValuePair<string, IDataParameter[]> kvp = dbDestK3Con.CreateCondParam(new Dictionary<string, object>() { { "TableName", "t_item" }, { "OrgKeyValue", strOrgFItemID } });
            string strSql = string.Format("select DestKeyValue from mkeymap where {0}", kvp.Key);

            string strFitemid = dbDestK3Con.GetValue(strSql, kvp.Value);
            if (strFitemid.Length > 0)
            {
                return strFitemid;
            }

            Update(dbOrgK3Con, dbDestK3Con, strOrgFItemID);

            kvp = dbDestK3Con.CreateCondParam(new Dictionary<string, object>() { { "TableName", "t_item" }, { "OrgKeyValue", strOrgFItemID } });
            return dbDestK3Con.GetValue(strSql, kvp.Value);
        }

        //public override void Delete(DBAccess dbOrgCon, DBAccess dbCon, string strKeyValue)
        //{
        //    throw new MaxLeafException("Delete 上级组 没实现");
        //}
    }
}
